﻿using Duende.IdentityServer;
using Duende.IdentityServer.Models;
using Duende.IdentityServer.Services;
using Duende.IdentityServer.Stores;
using Duende.IdentityServer.Stores.Serialization;
using Duende.IdentityServer.Validation;
using Service.Auth.Domain.AggregatesModel;
using Service.Core.Redis.Services;
using Service.Framework.Utils;
using Service.Auth.Api.Application.Query;

namespace Service.Auth.Api.Application.IdsValidator
{
    /// <summary>
    /// 
    /// </summary>
    public class CustomRefreshTokenService : DefaultRefreshTokenService
    {
        private readonly UserQueryService _userQueryService;
        private readonly IRedisService _redisService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="refreshTokenStore"></param>
        /// <param name="profile"></param>
        /// <param name="clock"></param>
        /// <param name="options"></param>
        /// <param name="logger"></param>
        /// <param name="redisService"></param>
        public CustomRefreshTokenService(IRefreshTokenStore refreshTokenStore, IProfileService profile, IClock clock, PersistentGrantOptions options, ILogger<DefaultRefreshTokenService> logger, IRedisService redisService, UserQueryService userQueryService) : base(refreshTokenStore, profile, clock, options, logger)
        {
            _redisService = redisService;
            _userQueryService = userQueryService;
        }

        public override async Task<TokenValidationResult> ValidateRefreshTokenAsync(string tokenHandle, Client client)
        {
            var result = await base.ValidateRefreshTokenAsync(tokenHandle, client);
            if (result.IsError || result.RefreshToken?.SubjectId == null ||
                result.RefreshToken.SubjectId.IsNullOrWhiteSpace()) return result;
            return result;
        }
    }
}
